package com.wnzt.baszh.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wnzt.baszh.dao.IPrintDao;
import com.wnzt.baszh.util.common.ConstantsUtils;
import com.wnzt.baszh.util.common.PageHelper;
import com.wnzt.baszh.util.common.PageHelperService;

/**
 * 打印DAO实现类
 * @author dqw
 *
 */
@Repository
public class PrintDaoImpl extends PageHelperService implements IPrintDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public PageHelper printDataQuery(Map<String, Object> map) {
		String date1 = (String) map.get("date1");
		String date2 = (String) map.get("date2");
		String fcydept = (String) map.get("fcydept");
		String fprn = (String) map.get("fprn");
		String ftimes = (String) map.get("ftimes");
		String fbarcode = (String) map.get("fbarcode");
		String fname = (String) map.get("fname");
		String fsex = (String) map.get("fsex");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		StringBuffer sb = new StringBuffer(" 1=1 ");
		if(ConstantsUtils.isNotBank(date1)){
			sb.append(" and a.fcydate >= '"+date1+"' ");
		}else if(ConstantsUtils.isNotBank(date2)){
			sb.append(" and a.fcydate < '"+date2+"' ");
		}else if(ConstantsUtils.isNotBank(fprn)){
			sb.append(" and a.fprn = '"+fprn+"'");
		}else if(ConstantsUtils.isNotBank(ftimes)){
			sb.append(" and a.ftimes = "+ftimes+"");
		}else if(ConstantsUtils.isNotBank(fbarcode)){
			sb.append(" and a.fbarcode = '"+fbarcode+"'");
		}else if(ConstantsUtils.isNotBank(fname)){
			sb.append(" and a.fname = '"+fname+"'");
		}else if(ConstantsUtils.isNotBank(fsex)){
			if(fsex.equals("0")){
				sb.append("");
			}else if(fsex.equals("1")){
				sb.append(" and a.fsex = '男'");
			}else{
				sb.append(" and a.fsex = '女'");
			}
		}
		if(ConstantsUtils.isNotBank(fcydept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + fcydept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            sb.append(" and a.fcytykh in ('" + departNumStr + "')");
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
		if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
        String field = " a.fprn ,a.ftimes,a.fname,a.fzyid,a.fsex,a.fbirthday,a.fbarcode,a.fcydate,a.frydate,a.frydept,a.fcydept,a.fcydept,a.fcybs,a.fidcard  ";
        String table = " szh_base a ";
		return this.queryMssqlSplitPageByProc(field, table ,sb.toString(), "a.fzyid", 1, "a.fzyid", currentPage, pageSize);
	}

	@Override
	public boolean printRegister(Map<String, Object> map) {
		String fcydate = (String) map.get("fcydate");
		String purpose = (String) map.get("purpose");
		String purposeText = (String) map.get("purposeText");
		String relationText = (String) map.get("relationText");
		String relation = (String) map.get("relation");
		String IDCard = (String) map.get("IDCard");
		String printMan = (String) map.get("printMan"); //代理人姓名
		String address = (String) map.get("address");
		String fzyid = (String) map.get("fzyid");
		String fname = (String) map.get("fname"); //当前病人姓名
		String date = (String) map.get("date");
		String curName = (String) map.get("curName");
		String fcode = (String) map.get("fcode");
		String content = (String) map.get("content");
		String fyr = "";
		//如果代理人不为空，则数据库中的复印人为代理人姓名；如果为空，则为当前病人姓名
		if(ConstantsUtils.isNotBank(printMan)){
			fyr = printMan;
		}else{
			fyr = fname;
		}
		if(ConstantsUtils.isNotBank(fzyid)){
			String sql = " insert into basz_fyjl(FZYID,FCYDATE, FFYR, FFYMDBH, FFYMD, FFYRQ, FYHZGX, FCZYBH, FCZY,"
					+ "	FSQFYRQ, FZJDZ, FDLRIDCARD, FFYNR,FDLRNAME, FCHARGE)"
					+ " values('"+fzyid+"','"+fcydate+"','"+fyr+"','"+purpose+"','"+purposeText+"','"+date+"','"+relationText
					+"','"+fcode+"','"+curName+"','"+date+"','"+address+"','"+IDCard+"','"+content+"','"+fyr+"','1')";
			this.jdbcTemplate.execute(sql);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean savePrintCharge(Map<String, Object> map) {
		String name = (String) map.get("name");
		String fzyid = (String) map.get("fzyid");
		String page = (String) map.get("page");
		String copies = (String) map.get("copies");
		String total = (String) map.get("total");
		String date = (String) map.get("date");
		if(ConstantsUtils.isNotBank(fzyid)){
			String sql = "update basz_fyjl set ffyfy = '"+total+"' , ffs = '"+page+"',fzs = '"+copies+"',ffyrq = '"+date+"',"
					+ "ffyr = '"+name+"',fcharge = '0' where fzyid = '"+fzyid+"' ";
			this.jdbcTemplate.update(sql);
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public List<Map<String, Object>> getFcybsByKsbh(String ksbh, String fcode) {
		String sql ="";
		String sql2 = "select fbqdm,fbqmc,fworkbq from tsysuser where fcode = '"+fcode+"'";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql2);
		List<Map<String, Object>> resultList = null;
		if(list.size() > 0){
			for (Map<String, Object> map : list) {
				if( map.get("fworkbq") != null){
					String str = (String) map.get("fworkbq");
					String bq = str.replace("\"", "'");
					if(ConstantsUtils.isNotBank(ksbh)){
						sql = "select  fhiskh,fhisksname from THISKSSET where FKSTYPE=81 AND FTYKH= '"+ksbh+"'  ";
					}else{
						sql = "select fhiskh,fhisksname from THISKSSET where FKSTYPE=81 and FTYKH in("+bq+")";
					}
					resultList = this.jdbcTemplate.queryForList(sql);
				}
			}
		}
		return resultList;
	}
	
	@Override
	public PageHelper fastPrintQuery(Map<String, Object> map) {
		String date1 = (String) map.get("date1");
		String date2 = (String) map.get("date2");
		String fcydept = (String) map.get("fcydept");
		String fcybq = (String) map.get("fcybq");
		String fprn = (String) map.get("fprn");
		String fname = (String) map.get("fname");
		String fzydoc = (String) map.get("fzydoc");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		StringBuffer sb = new StringBuffer(" 1=1 ");
		if(ConstantsUtils.isNotBank(date1)){
			sb.append(" and a.fcydate >= '"+date1+"' ");
		}else if(ConstantsUtils.isNotBank(date2)){
			sb.append(" and a.fcydate < '"+date2+"' ");
		}else if(ConstantsUtils.isNotBank(fcybq)){
			sb.append(" and a.fprn = '"+fcybq+"'");
		}else if(ConstantsUtils.isNotBank(fprn)){
			sb.append(" and a.ftimes = "+fprn+"");
		}else if(ConstantsUtils.isNotBank(fname)){
			sb.append(" and a.fbarcode = '"+fname+"'");
		}else if(ConstantsUtils.isNotBank(fzydoc)){
			sb.append(" and a.fname = '"+fzydoc+"'");
		}
		if(ConstantsUtils.isNotBank(fcydept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + fcydept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            sb.append(" and a.fcytykh in ('" + departNumStr + "')");
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
		if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
        String field = " a.fname,a.fprn,a.fcydate,a.fsex,a.fage,a.fcydept,a.fcydept,a.fzzdoct,a.ftimes,a.fidcard,a.frydate,a.fbarcode  ";
        String table = " szh_base a ";
		return this.queryMssqlSplitPageByProc(field, table ,sb.toString(), "a.fprn", 0, "a.fprn", currentPage, pageSize);
	}
	
	@Override
	public PageHelper batchPrintQuery(Map<String, Object> map) {
		String date1 = (String) map.get("date1");
		String date2 = (String) map.get("date2");
		String fcydept = (String) map.get("fcydept");
		String fcybq = (String) map.get("fcybq");
		String fprn = (String) map.get("fprn");
		String fname = (String) map.get("fname");
		String fzydoc = (String) map.get("fzydoc");
		String page = (String) map.get("page");
		String rows = (String) map.get("rows");
		int pageSize = 10;
		int currentPage = 1;
		StringBuffer sb = new StringBuffer(" 1=1 ");
		if(ConstantsUtils.isNotBank(date1)){
			sb.append(" and a.fcydate >= '"+date1+"' ");
		}else if(ConstantsUtils.isNotBank(date2)){
			sb.append(" and a.fcydate < '"+date2+"' ");
		}else if(ConstantsUtils.isNotBank(fcybq)){
			sb.append(" and a.fprn = '"+fcybq+"'");
		}else if(ConstantsUtils.isNotBank(fprn)){
			sb.append(" and a.ftimes = "+fprn+"");
		}else if(ConstantsUtils.isNotBank(fname)){
			sb.append(" and a.fbarcode = '"+fname+"'");
		}else if(ConstantsUtils.isNotBank(fzydoc)){
			sb.append(" and a.fname = '"+fzydoc+"'");
		}
		if(ConstantsUtils.isNotBank(fcydept)){
			 String departNumSql = "select fcal from tworkroom where ftykh='" + fcydept + "'";
	            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
	            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
	            sb.append(" and a.fcytykh in ('" + departNumStr + "')");
		}
		if (ConstantsUtils.isNotBank(page)) {
            currentPage = Integer.valueOf(page);
        }
		if (ConstantsUtils.isNotBank(rows)) {
            pageSize = Integer.valueOf(rows);
        }
        String field = " convert(char(10),getdate(),120) fsqdate,a.fname,a.fprn,a.fcydate,a.fsex,a.fage,a.fcydept,a.fcydept,a.fzzdoct,a.ftimes,a.fidcard,a.frydate,a.fbarcode  ";
        String table = " szh_base a ";
		return this.queryMssqlSplitPageByProc(field, table ,sb.toString(), "a.fprn", 0, "a.fprn", currentPage, pageSize);
	}
	
	  @Override
	    public PageHelper copyPrintRecordSearch(Map<String, Object> map) {
		  String FCYDATE = (String) map.get("FCYDATE");                //日期类型(复印申请日期、出院日期)
	        String FQSDATE = (String) map.get("FQSDATE");                //起始日期()
	        String FJSDATE = (String) map.get("FJSDATE");                //结束日期()
	        String FCYKS = (String) map.get("FCYKS");                 	 //出院科室
	        
	        String FCZY = (String) map.get("FCZY");                  	 //操作员
	        String FPRNTEXT = (String) map.get("FPRNTEXT");        	     //病案号值,同时将中文逗号转英文逗号
	        String FNAME = (String) map.get("FNAME");                    //姓名
	         
	        String FSTATE = (String) map.get("FSTATE");                  //状态()
	        String page = (String) map.get("page");
			String rows = (String) map.get("rows");
			int pageSize = 10;
			int currentPage = 1;
			StringBuffer sb = new StringBuffer(" 1=1 ");    
	        
	     
			if(ConstantsUtils.isNotBank(FQSDATE)){
				sb.append(" and a.fcydate >= '"+FQSDATE+"' ");
			}else if(ConstantsUtils.isNotBank(FJSDATE)){
				sb.append(" and a.fcydate < '"+FJSDATE+"' ");
			}else if(ConstantsUtils.isNotBank(FCZY)){
				sb.append(" ");
			}else if(ConstantsUtils.isNotBank(FPRNTEXT)){
				 String valSql = "";
		            
		            String fprnArray [] = FPRNTEXT.split(",");
		            
		            for (int i = 0; i < fprnArray.length; i++) {
		                
		                //判断分割后的病案号值中存在%
		                if (fprnArray[i].indexOf('%')>-1) {
		                    
		                    if (i == fprnArray.length-1) {
		                        valSql += " t.fprn like ''"+fprnArray[i]+"''";
		                        break;
		                    }
		                    valSql += " t.fprn like ''"+fprnArray[i]+"'' or";
		                    
		                }else {
		                    if (i == fprnArray.length-1) {
		                        valSql += " t.fprn = ''"+fprnArray[i]+"''";
		                        break;
		                    }
		                    valSql += " t.fprn = ''"+fprnArray[i]+"'' or";
		                }
		            }
		            String fprnSql = " and ("+valSql+")";
		            sb.append(fprnSql);
			}else if(ConstantsUtils.isNotBank(FNAME)){
				String valSql = "";
	            
	            String fnameArray [] = FNAME.split(",");
	            
	            for (int i = 0; i < fnameArray.length; i++) {
	                
	                //判断分割后的姓名值中存在%
	                if (fnameArray[i].indexOf('%')>-1) {
	                    
	                    if (i == fnameArray.length-1) {
	                        valSql += " t.fname like ''"+fnameArray[i]+"''";
	                        break;
	                    }
	                    valSql += " t.fname like ''"+fnameArray[i]+"'' or";
	                    
	                }else {
	                    if (i == fnameArray.length-1) {
	                        valSql += " t.fname = ''"+fnameArray[i]+"''";
	                        break;
	                    }
	                    valSql += " t.fname = ''"+fnameArray[i]+"'' or";
	                }
	            }
	            String fnameSql = " and ("+valSql+")";
	            sb.append(fnameSql);
			}else if(ConstantsUtils.isNotBank(FSTATE)){
				sb.append(" ");
			}
			
			if(ConstantsUtils.isNotBank(FCYKS)){
				 String departNumSql = "select fcal from tworkroom where ftykh='" + FCYKS + "'";
		            Map<String, Object> departNumMap = this.jdbcTemplate.queryForMap(departNumSql);
		            String departNumStr = departNumMap.get("fcal").toString().replace(",", "','");
		            sb.append(" and a.fcytykh in ('" + departNumStr + "')");
			}
			if (ConstantsUtils.isNotBank(page)) {
	            currentPage = Integer.valueOf(page);
	        }
			if (ConstantsUtils.isNotBank(rows)) {
	            pageSize = Integer.valueOf(rows);
	        }
			
	        String field = " a.fname,a.fprn,a.fcydate,a.fsex,a.fage,a.fcydept,a.fcydept,a.fzzdoct,a.ftimes,a.fidcard,a.frydate,a.fbarcode  ";
	        String table = " szh_base a ";
			return this.queryMssqlSplitPageByProc(field, table ,sb.toString(), "a.fprn", 0, "a.fprn", currentPage, pageSize);
	    }
	  
	  @Override
	    public Object printPurposeSet() {
	        String querySql = "select t.* from SZH_PRINT t where 1=1";
	        
	        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(querySql);
	        return list;
	    }

	    @Override
	    public  Object  showPrintSetOrder(String fid) {
	    	String querySql = "select t.FID,t.fcode,t.fchildrenname FORDERBH from SZH_PRINTCHILD t  where t.fcode="+fid;
	        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(querySql);
	        return list;
	    }
		
		@Override
		public Object addPrintPurpose(String fCODE, String fNAME, String fZJC) {
			boolean b = true;
			String addSql = "insert into SZH_PRINT(FCODE,FNAME,FZJC) VALUES('"+fCODE+"','"+fNAME+"','"+fZJC+"')";
			try {
				this.jdbcTemplate.execute(addSql);
			} catch (Exception e) {
				b = false;
			}
			return b;
		}
		
		@Override
		public Object editPrintPurpose(String fID, String fCODE, String fNAME, String fZJC) {
			boolean b = true;
			String updateSql = "update SZH_PRINT set FCODE='"+fCODE+"',FNAME='"+fNAME+"',FZJC='"+fZJC+"' where fid="+fID;
			try {
				this.jdbcTemplate.execute(updateSql);
			} catch (Exception e) {
				b = false;
			}
			return b;
		}

		@Override
		public Object deletePrintPurpose(String fID) {
			boolean b = true;
			String deleteSql = "delete from SZH_PRINT where fid="+fID;
			String deleteSql2 = "delete from SZH_PRINTCHILD where fcode="+fID;
			try {
				this.jdbcTemplate.execute(deleteSql);
				this.jdbcTemplate.execute(deleteSql2);
			} catch (Exception e) {
				b = false;
			}
			System.out.println(deleteSql);
			System.out.println(deleteSql2);
			return b;
		}
		
		@Override
		public Object checkAllPrintPurpose() {
			String querySql = "select t.FCODE,t.FNAME from SZH_PRINT t where 1=1";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(querySql);
			return list;
		}
}
